<template>
  <div>
    <div class="action-head">
      <el-radio-group v-model="collect" size="mini" @change="onChange" >
        <el-radio-button :label="false" >全部</el-radio-button>
        <el-radio-button :label="true" >收藏</el-radio-button>
      </el-radio-group>
      <el-button
        v-if="isShowAdd"
        type="success"
        size="mini"
        @click="dialogVisible = !dialogVisible"
        >上传素材</el-button
      >
    </div>
    <el-row :gutter="20">
      <el-col
        :xs="12"
        :sm="6"
        :md="6"
        :lg="4"
        v-for="(item, index) in images"
        :key="item.id"
        class="image-item"
        @click.native="onSelected(index)"
      >
        <el-image
          :style="{
            width: isPublish === true ? '120px' : '241px',
            height: '120px'
          }"
          :src="item.url"
          fit="cover"
        >
          <div
            slot="placeholder"
            style="display: flex; justify-content: center; padding-top: 50px"
          >
            加载中<span class="dot">...</span>
          </div>
        </el-image>
        <el-image>
          <div slot="error" class="image-slot"></div>
        </el-image>
        <div
          class="seletcted"
          v-if="isShowSelected && selected === index"
        ></div>
        <div class="image-action" v-if="isShowAction">
          <el-button
            :loading="item.loading"
            size="small"
            @click="onCollect(item)"
            type="warning"
            :class="{
              'el-icon-star-on': item.is_collected,
              'el-icon-star-off': !item.is_collected
            }"
            circle
          ></el-button>
          <!-- <i @click="onCollect(item)" :class="{ 'el-icon-star-on':item.is_collected,'el-icon-star-off': !item.is_collected}"></i> -->
          <!-- <i class="el-icon-delete-solid"></i> -->
          <el-button
            :loading="item.loading"
            size="small"
            @click="onDelete(item)"
            type="danger"
            icon="el-icon-delete-solid"
            circle
          ></el-button>
        </div>
      </el-col>
    </el-row>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-size="pageSize"
      background
      layout="total, prev, pager, next"
      :total="total"
      :disabled="loading"
    >
    </el-pagination>
    <el-dialog
      title="上传图片"
      :visible.sync="dialogVisible"
      width="22%"
      :append-to-body="true"
    >
      <el-upload
        drag
        action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
        multiple
        :headers="uploadHeaders"
        name="image"
        :on-success="onUploadSuccess"
        :show-file-list="true"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          只能上传jpg/png文件，且不超过500kb
        </div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { getImages, collectImage, deleteImage } from '@/api/image'
export default {
  name: 'ImageIndex',
  props: {
    isShowAdd: {
      type: Boolean,
      default: true
    },
    isShowAction: {
      type: Boolean,
      default: true
    },
    isShowSelected: {
      type: Boolean,
      default: false
    },
    isPublish: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const user = JSON.parse(window.localStorage.getItem('user'))
    return {
      images: [],
      collect: 'false',
      total: 0,
      page: 1,
      pageSize: 12,
      dialogVisible: false,
      uploadHeaders: {
        Authorization: `Bearer ${user.token}`
      },
      selected: null,
      loading: false
    }
  },
  created() {
    this.loadImages()
  },
  methods: {
    loadImages(page) {
      this.loading = true
      getImages({
        collect: this.collect,
        page: page,
        per_page: this.pageSize
      }).then((res) => {
        const results = res.data.data.results
        results.forEach((img) => {
          img.loading = false
        })
        this.pageSize = res.data.data.per_page
        this.images = results
        this.total = res.data.data.total_count
        this.loading = false
      })
    },
    onChange() {
      this.loadImages(1)
    },
    onUploadSuccess() {
      this.dialogVisible = false
      this.loadImages(this.page)
    },
    handleCurrentChange(newpage) {
      this.selected = null
      this.loadImages(newpage)
    },
    onCollect(img) {
      img.loading = true
      collectImage(img.id, !img.is_collected).then((res) => {
        img.is_collected = !img.is_collected
        img.loading = false
        this.$message.success(img.is_collected ? '收藏成功！' : '取消收藏！')
      })
    },
    onDelete(img) {
      img.loading = true
      deleteImage(img.id).then((res) => {
        if (res.status !== 204) {
          this.$message.eooro('删除失败！')
        } else {
          img.loading = false
          this.loadImages(this.page)
          this.$message.success('删除成功！')
        }
      })
    },
    onSelected(index) {
      this.selected = index
    }
  }
}
</script>
<style lang="less" scoped>
.action-head {
  padding-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
.image-item {
  position: relative;
}
.image-action {
  height: 40px;
  background-color: rgba(204, 204, 204, 0.5);
  position: absolute;
  bottom: 3px;
  left: 10px;
  right: 10px;
  font-size: 25px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  color: #fff;
}
.el-pagination {
  padding-top: 18px;
}
.seletcted {
  background: url(./selected.png) no-repeat;
  background-size: cover;
  width: 120px;
  height: 120px;
  position: absolute;
  left: 10px;
  right: 0;
  bottom: 0;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
